package com.situ.feemanagement.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.feemanagement.mapper.MedicineMapper;
import com.situ.feemanagement.mapper.OrderMapper;
import com.situ.feemanagement.pojo.entity.Medicine;
import com.situ.feemanagement.pojo.entity.Order;
import com.situ.feemanagement.pojo.query.OrderQuery;
import com.situ.feemanagement.pojo.vo.OrderVO;
import com.situ.feemanagement.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private MedicineMapper medicineMapper;

    @Override
    public Map<String, Object> selectByPage(OrderQuery orderQuery) {
        PageHelper.startPage(orderQuery.getPage(), orderQuery.getLimit());
        List<OrderVO> list = orderMapper.selectByPage(orderQuery);
        PageInfo<OrderVO> pageInfo = new PageInfo<>(list);
        int totalCount = (int) pageInfo.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("total", totalCount);
        map.put("list", list);
        return map;
    }

    @Override
    public void deleteById(int id) {
        Order order = orderMapper.selectById(id);
        Medicine medicine = medicineMapper.selectById(order.getMedicineId());
        int inventory = medicine.getInventory() + order.getNumber();
        medicineMapper.updateInventory(order.getMedicineId(), inventory);
        orderMapper.deleteById(id);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            Order order = orderMapper.selectById(id);
            Medicine medicine = medicineMapper.selectById(order.getMedicineId());
            int inventory = medicine.getInventory() + order.getNumber();
            medicineMapper.updateInventory(order.getMedicineId(), inventory);
            orderMapper.deleteById(id);
        }
    }

    @Override
    public void add(Order order) {
        orderMapper.add(order);
        Medicine medicine = medicineMapper.selectById(order.getMedicineId());
        int inventory = medicine.getInventory() - order.getNumber();
        medicineMapper.updateInventory(order.getMedicineId(), inventory);
    }

    @Override
    public Order selectById(int id) {
        return orderMapper.selectById(id);
    }

    @Override
    public void update(Order order) {
        Order oldOrder = orderMapper.selectById(order.getId());
        Medicine oldMedicine = medicineMapper.selectById(oldOrder.getMedicineId());
        int oldInventory = oldMedicine.getInventory() + oldOrder.getNumber();
        medicineMapper.updateInventory(oldMedicine.getId(), oldInventory);
        Medicine newMedicine = medicineMapper.selectById(oldOrder.getMedicineId());
        int newInventory = newMedicine.getInventory() - order.getNumber();
        medicineMapper.updateInventory(newMedicine.getId(), newInventory);
        orderMapper.update(order);
    }
}
